<?php

namespace yak\layui\widgets\form;

use yii\helpers\Html;
use yii\widgets\InputWidget;

/**
 * Class IconPicker
 * @package yak\layui\widgets\form
 */
class IconPicker extends InputWidget
{
    public $type = 0; // 0 渲染 1 click

    public function run()
    {
        $this->options['id'] = $this->options['lay-filter'] = $this->getId();

        return call_user_func_array([$this, 'render' . $this->type], []);
    }

    public function render1()
    {
        $id = $this->getId();
        echo Html::activeHiddenInput($this->model, $this->attribute, ['id' => $id . '-icon-pick']);
        return Html::button('图标选择', [
            'class' => 'layui-btn icon-picker-click',
            'data-target' => $id . '-icon-pick'
        ]);
    }

    public function render0()
    {
        $js = <<<JS
        
        layui.use(['element','form', 'jquery', 'IconFonts'], function () {
            var element = layui.element;
            var form = layui.form;
            var $ = layui.jquery;
            var IconFonts=layui.IconFonts;
    
            //图标选择器
            IconFonts.render({
                // 选择器，推荐使用input
                elem: '#{$this->options['id']}', //选择器ID
                // 数据类型：fontClass/layui_icon，
                type: 'layui_icon',
                // 是否开启搜索：true/false
                search: true,
                // 是否开启分页
                page: true,
                // 每页显示数量，默认12
                limit: 12,
                // 点击回调
                click: function (data) {
                    //console.log(data);
                }
            });
    
           /**
             * 默认选选中图标
             */
            // IconFonts.checkIcon("iconFonts","","layui_icon");
        });

JS;

        $this->view->registerJs($js, \yii\web\View::POS_END);

        $this->options['lay-filter'] = 'iconFonts';
        $this->options['class'] = 'layui-input';

        return Html::activeTextInput($this->model, $this->attribute, $this->options);
    }
}